package com.appos.web.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.appos.common.core.controller.BaseController;
import com.appos.common.core.domain.AjaxResult;
import com.appos.common.core.page.TableDataInfo;
import com.appos.domain.DynamicCollections;
import com.appos.service.MongoDbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class DynamicCollectionsController extends BaseController {
    @Autowired
    private MongoDbService mongoDbService;

    @PostMapping("/dynamicCollections/save")
    public AjaxResult save(@RequestBody JSONObject jsonObject){
        String collectionsName = (String) jsonObject.get("collectionsName");

        DynamicCollections dynamicCollections = new DynamicCollections();
        String uuid = (String) jsonObject.get("uuid");
        dynamicCollections.setUuid(uuid);
        JSONObject widgetModels = (JSONObject) JSONObject.parseObject(JSONObject.toJSONString(jsonObject.get("widgetModels")));
        dynamicCollections.setWidgetModels(widgetModels);
        return toAjax(mongoDbService.createAutoTask(collectionsName, dynamicCollections));
    }

    @GetMapping("/dynamicCollections/list")
    public TableDataInfo list(@RequestParam String collectionsName) {

        TableDataInfo rspData = mongoDbService.findList(collectionsName);
        return rspData;
    }

    @PostMapping("/dynamicCollections/delete")
    @ResponseBody
    public AjaxResult delete(@RequestBody JSONObject jsonObject) {
        String collectionsName = (String) jsonObject.get("collectionsName");
        JSONArray uuids = (JSONArray) JSONArray.parseArray(JSONObject.toJSONString(jsonObject.get("uuids")));
        return AjaxResult.success(mongoDbService.del(collectionsName, uuids));
    }
}
